package com.wenhuitec.android.tang.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.dhkj.web.bean.AddrTree;
import com.dhkj.web.utils.MyObserverRes;
import com.dhkj.web.utils.RequestUtils;
import com.wenhuitec.android.tang.R;
import com.wenhuitec.android.tang.config.SystemInit;
import com.wenhuitec.android.tang.sysui.model.PickModel;
import com.wenhuitec.android.tang.sysui.view.PickView;
import com.wenhuitec.android.tang.sysui.view.SmNumView;
import com.wenhuitec.android.tang.ui.adapter.KeyAdapter;
import com.wenhuitec.android.tang.util.Calculate;

import java.util.ArrayList;
import java.util.List;

public class Activity4 extends AppCompatActivity {
    private GridView mGridView; //数字键盘
    private List<Integer> listNumber;
    private Activity mContext;
    private SmNumView sumKey;
    private List<AddrTree> addrTreeList;
    private PickView addressPv;
    private PickView shopPv;
    private String number;
    private String numberTemp;
    private TextView numberTempTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cash);
        mContext = this;
        mGridView = (GridView) findViewById(R.id.gv_pass);
        addressPv = findViewById(R.id.addressPv);
        sumKey = findViewById(R.id.sumKey);
        shopPv = findViewById(R.id.shopPv);
        numberTempTv = findViewById(R.id.numberTempTv);
        number = "";
        numberTemp = "";
        findViewById(R.id.scanQrBut).setOnClickListener(v -> {
            startActivity(new Intent(mContext, ScanQrActivity.class));
        });

        initData();
        addrTreeList = SystemInit.getInstance(mContext).getAddrTreeList();
        initAddress();
    }

    KeyAdapter.KeyOnClick keyOnClick = new KeyAdapter.KeyOnClick() {
        @Override
        public void onClick(Integer keynum) {

            if (keynum >= 0 && keynum <= 9) {
                number = number + keynum;
            } else if (keynum == 102) {
                if (numberTemp.length() > 0 && number.length() == 0) {
                    numberTemp = numberTemp.substring(0, numberTemp.length() - 1);
                }
                numberTemp = numberTemp + number + "-";
                number = "";
            } else if (keynum == 101) {
                if (numberTemp.length() > 0 && number.length() == 0) {
                    numberTemp = numberTemp.substring(0, numberTemp.length() - 1);
                }
                numberTemp = numberTemp + number + "+";
                number = "";
            } else if (keynum == 103) {
                Calculate t = new Calculate();
                if (number.length() == 0) {
                    number = "0";
                }
                numberTemp = numberTemp + number;
                String jieguo = t.calculate(numberTemp);
                numberTemp = "";
                number = jieguo;
            } else if (keynum == 104) {
                numberTemp = "";
                number = "";
            }
            numberTempTv.setText(numberTemp);
            sumKey.setText(number);
        }
    };


    /**
     * isRandom是否开启随机数
     */
    private void initData() {
        KeyAdapter keyAdapter = new KeyAdapter(mContext);
        keyAdapter.setKeyClick(keyOnClick);
        mGridView.setAdapter(keyAdapter);
    }


    private void initAddress() {
        addressPv.setTitle("地点：");
        List<PickModel> pickModelList = new ArrayList<>();
        for (AddrTree goo : addrTreeList) {
            pickModelList.add(new PickModel(goo.getId(), goo.getAddrName().trim()));
        }
        addressPv.setRv(pickModelList);
        addressPv.setOnClick((pickModel,position) -> {
            initShop(pickModel.getId());
        });
    }

    private void initShop(String id) {
        shopPv.setTitle("商铺：");
        AddrTree addrTree = getAddrTree(addrTreeList, id);
        List<PickModel> pickModelList = new ArrayList<>();
        if (addrTree != null) {
            for (AddrTree goo : addrTree.getChildren()) {
                pickModelList.add(new PickModel(goo.getId(), goo.getAddrName().trim()));
            }
        }
        shopPv.setRv(pickModelList);
    }

    private AddrTree getAddrTree(List<AddrTree> addrTreeList, String id) {
        for (AddrTree goo : addrTreeList) {
            if (goo.getId().equals(id)) {
                return goo;
            }
        }
        return null;
    }
}
